package com.example.myaudiorecord.Util;


import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.Environment;
import android.util.Log;
import android.widget.Chronometer;
import android.widget.ListView;

import com.example.myaudiorecord.MyAdapter.AudioRecordAdapter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AudioRecordUtil {
    private final Object object = new Object();


    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

    private Thread recordingThread = null;
    private FileOutputStream os;
    private static List<String> strings = new ArrayList<>();
    private boolean isRecording;

    public List<String> getListStrings() {
        return strings;
    }

    public void startRecording(AudioRecord recorder) {
        recorder.startRecording();
        isRecording = true;
        synchronized (object) {
            recordingThread = new Thread(new Runnable() {
                public void run() {
                    writeAudioDataToFile();
                }

                private void writeAudioDataToFile() {
                    String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis() + "/audio_record.pcm";
                    strings.add(filePath);
                    File file = new File(filePath);
                    Log.d("sss", filePath);
                    if (file.canRead() && file.exists()) {
                        Log.d("sss", "可读且可用");
                    }
                    byte[] data = new byte[AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING)];

                    FileOutputStream os = null;

                    try {
                        os = new FileOutputStream(file);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    int read;
                    if (null != os) {
                        while (isRecording) {
                            read = recorder.read(data, 0, data.length);
                            if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                                try {
                                    os.write(data);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, "AudioRecorder Thread");
            recordingThread.start();
        }
    }
    public void stopRecording(AudioRecord recorder){
        isRecording = false;
        recorder.stop();
    }
}
